<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/12
 * Time: 13:38
 */

namespace app\admin\controller;

/**
 * Class Label
 * @package app\admin\controller
 * 活动标签管理
 */
class Label extends Basic
{

    /**
     * 初始化
     */
    public function _initialize()
    {
        parent::_initialize();
    }
    /**
	 * 添加广告
	 * @author chengkaikai
	 */
    public function add_banner(){
		$label_id = input('label_id');
		$title = input('title');
		$type = input('type',1);
		$picture = input('picture');
		$url = input('url');
		$sort = input('sort',1);
		$status = input('status',1);
		if($type ==2) {
			if (!$label_id) {
				json_fail('缺少label_id参数');
			}
		}else{
			$label_id = 0; //首页banner
		}
		if (!$title) {
			json_fail('缺少标题参数');
		}
		if (!$picture) {
			json_fail('缺少图片参数');
		}

		//实例化数据库
		$banner = new \app\admin\model\Banner();

		//要添加的数据
		$addData = array(
			'pid'=>$label_id,
			'type'=>$type,
			'title'=>$title,
			'picture'=>$picture,
			'sort'=>$sort,
			'status'=>$status
		);
		if($url){
			$addData['url']=$url;
		}

		$banner_id = $banner->insertGetId($addData);
		if(!$banner_id){
			json_fail('存储失败');
		}
		json_success($banner_id);
	}

	/**
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 * 广告列表
	 * @author chengkaikai
	 */
	public function banner_list(){
		$label_id = input('label_id');
		$type = input('type',1);
        $page = input('page',1);
        $limit = input('limit',10);
		if($type ==2) {
			if (!$label_id) {
				json_fail('缺少label_id参数');
			}
			$map['pid'] = $label_id;
		}

		//实例化数据库
		$banner = new \app\admin\model\Banner();

		$map['status'] = array('neq',0);
		$map['type'] = $type;

		$infoList['list'] = $banner->where($map)->order('sort')->page($page,$limit)->select();
		$infoList['count'] = $banner->where($map)->count();

		if(!$infoList){
			json_success([]);
		}
		json_success($infoList);
	}

	/**
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 * @author chengkaikai
	 * 展示修改
	 */
	public function show_edit(){
		$banner_id = input('banner_id');
		if (!$banner_id) {
			json_fail('缺少广告id参数');
		}

		//实例化数据库
		$banner = new \app\admin\model\Banner();
		$label = new \app\admin\model\Label();

		$info = $banner->where(array('id'=>$banner_id))->find();

		if(!$info){
			json_fail('暂无数据');
		}
		if($info['type'] ==2){
			$info['label_name'] = $label->where(array('id'=>$info['pid']))->value('name');
		}
		json_success($info);
	}

	/**
	 * 修改广告
	 * @author chengkaikai
	 */
	public function edit_banner(){
		$banner_id = input('banner_id');
		$title = input('title');
		$picture = input('picture');
		$url = input('url');
		$sort = input('sort',1);
		$status = input('status',1);
		if(!$banner_id){
			json_fail('获取banner_id错误');
		}
		if (!$title) {
			json_fail('缺少标题参数');
		}
		if (!$picture) {
			json_fail('缺少图片参数');
		}


		//实例化数据库
		$banner = new \app\admin\model\Banner();

		//要修改的数据
		$saveData = array(
			'title'=>$title,
			'picture'=>$picture,
			'sort'=>$sort,
			'status'=>$status
		);
		if($url){
			$saveData['url']=$url;
		}

		$banner_id = $banner->where(array('id'=>$banner_id))->update($saveData);
		if($banner_id === false){
			json_fail('存储失败');
		}
		json_success('修改成功');
	}

	/**
	 * 删除广告
	 * @author chengkaikai
	 */
	public function delete_banner(){
		$banner_id = input('banner_id');

		if(!$banner_id){
			json_fail('获取banner_id错误');
		}

		//实例化数据库
		$banner = new \app\admin\model\Banner();

		//要修改的数据
		$saveData = array(
			'status'=>0
		);

		$banner_id = $banner->where(array('id'=>$banner_id))->update($saveData);
		if($banner_id === false){
			json_fail('删除失败');
		}
		json_success('删除成功');
	}
}